from PySide.QtGui import *

from core.base import *
from shinylist import ShinyList, ShinyListItem

class TransactionView(BaseView):
	def on_data_update(self, transactions):
		print 'TransactionView received update'
		slist = self.slist
		slist.clear()

		for transaction in transactions:
			item = ShinyListItem()
			
			time = transaction.get_sale_time()
			customer = transaction.get_customer()
			item.set_main_text('Sale made on %s' % time.toString('ddd MMMM d, yyyy'))
			item.set_sub_text('Sold to %s' % customer.get_name())
			slist.add_item(item)

	def init_ui(self):
		# The contents of the tabs are always widgets containing other widgets.
		transaction_layout = QVBoxLayout() # The layout for the contents of the transaction tab.
		timeframe_layout = QHBoxLayout() # The layout for the timeframe text and combo box
		transaction_instruction = QLabel() # instructional text for transactions
		add_transaction_button = QPushButton('Add a Transaction',self) # The button used to call the handling of adding new transactions
		transaction_timeframe = QComboBox() # The combo box to handle the timeframe for the list.
		transaction_caption = QLabel() # Caption for transactions

		self.slist = slist = ShinyList()

		# Let's format the transaction's instructions
		transaction_instruction.setText('Click on an individual transaction in order to see all details about it and edit those details.')

		# Then we'll format the combo box and its text
		transaction_caption.setText('Show transactions from')

		transaction_timeframe.addItem('2 weeks',None)
		transaction_timeframe.addItem('1 month',None)
		transaction_timeframe.addItem('2 months',None)
		transaction_timeframe.addItem('6 months',None)
		transaction_timeframe.addItem('1 year',None)
		transaction_timeframe.addItem('Forever',None)

		# Now let's put all the timeframe stuff into their own horizontal layout
		timeframe_layout.addWidget(transaction_caption)
		timeframe_layout.addWidget(transaction_timeframe)

		# Now let's put them all together
		transaction_layout.addWidget(transaction_instruction) # add the instructions
		transaction_layout.addLayout(timeframe_layout)
		transaction_layout.addWidget(slist)
		transaction_layout.addWidget(add_transaction_button)
		self.setLayout(transaction_layout) # Set the layout of the transaction contents to its respective layout

	def __init__(self, parent=None, controller=None):
		super(TransactionView, self).__init__(parent, controller)
		self.init_ui()


class TransactionViewAddDialog(QDialog):
	def init_ui(self):
		view_layout = QVBoxLayout()
		option_layout = QHBoxLayout()
		
		customer_list = QComboBox()
		customer_caption = QLabel()
		customer_layout = QHBoxLayout()
		
		item_list = QComboBox()
		item_caption = QLabel() 
		item_layout = QHBoxLayout()

		stock_serial = QComboBox()
		serial_caption = QLabel()
		serial_layout = QHBoxLayout()
		
		# Buttons
		new_transaction_button = QPushButton('New Customer',self)
		save_transaction_button = QPushButton('Save',self)
		cancel_transaction_button = QPushButton('Cancel',self)
		
		customer_caption.setText('Customer Name')
		customer_layout.addWidget(customer_caption)
		customer_layout.addWidget(customer_list)
		
		serial_caption.setText('Stock Serial')
		serial_layout.addWidget(serial_caption)
		serial_layout.addWidget(stock_serial)
		
		item_caption.setText('Item Name')
		item_layout.addWidget(item_caption)
		item_layout.addWidget(item_list)
						
		option_layout.addWidget(new_transaction_button)
		option_layout.addWidget(save_transaction_button)
		option_layout.addWidget(cancel_transaction_button)
		
		view_layout.addLayout(customer_layout)
		view_layout.addLayout(item_layout)
		view_layout.addLayout(serial_layout)
		view_layout.addLayout(option_layout)
		self.setLayout(view_layout)
		

	def __init__(self, parent=None):
		super(TransactionViewAddDialog, self).__init__(parent)
		self.init_ui()
		

def main():
	import sys
	app = QApplication(sys.argv)

	wnd = TransactionView()
	wnd.setWindowTitle('Transaction View')
	wnd.show()

	sys.exit(app.exec_())

if __name__ == '__main__':
	main()
